import pygame
from pygame.sprite import Group

import game_functions as gf
from button import Button
from game_stats import GameStats
from scoreboard import ScoreBoard
from settings import Settings
from ship import Ship
from sound import Sound


def run_game():
    """游戏入口"""
    # 初始化游戏并创建一个屏幕对象
    pygame.init()
    # 启动mixer进程,用于加载音效
    pygame.mixer.init()
    # 游戏配置加载
    ai_settings = Settings()
    # 屏幕设置
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption(ai_settings.caption)

    # 创建音效
    sound = Sound()
    # 创建飞船
    ship = Ship(ai_settings, screen)
    # 创建存储子弹的编组,相当是一个列表，管理所有子弹
    bullets = Group()
    aliens = Group()
    # 创建aliens群
    gf.create_fleet(ai_settings, screen, aliens, ship)
    # 创建游戏统计实例
    stats = GameStats(ai_settings)
    # 创建Play按钮
    play_button = Button(ai_settings, screen, "Play")
    # 显示得分板
    score_board = ScoreBoard(ai_settings, screen, stats)

    while True:
        # 监视键盘和鼠标事件
        gf.check_events(ai_settings, screen, ship, bullets, play_button, stats, aliens, score_board, sound)
        # 确定游戏处于活动中时需要运行的代码
        if stats.game_active:
            # 移动飞船
            ship.update()
            # 子弹移动
            gf.update_bullet(bullets, aliens, ai_settings, screen, ship, stats, score_board)
            # alien 移动
            gf.update_alien(ai_settings, aliens, ship, stats, screen, bullets, score_board)
        # 屏幕刷新
        gf.update_screen(screen, ai_settings, ship, bullets, aliens, play_button, stats, score_board)


run_game()
